#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int INF = 2e9;
int dist[N];
bool st[N];
vector<PII> edges[N];
int n, m, s;
// 时间复杂度O(n * logn)
void dijkstra(int s)
{
    for(int i = 0; i <= n; i++) dist[i] = INF;
    priority_queue<PII, vector<PII>, greater<PII>> q;
    dist[s] = 0;
    q.push({0, s});
    while(q.size())
    {
        auto t = q.top(); q.pop();
        int u = t.second;
        if(st[u]) continue;
        st[u] = true;
        for(auto t : edges[u])
        {
            int v = t.first, w = t.second;
            if(dist[v] > dist[u] + w)
            {
                dist[v] = dist[u] + w;
                q.push({dist[v], v});
            }
        }
    }
    for(int i = 1; i <= n; i++) cout << dist[i] << " ";
}
int main()
{
    cin >> n >> m >> s;
    for(int i = 1; i <= m; i++)
    {
        int u, v, w; cin >> u >> v >> w;
        edges[u].push_back({v, w});
    }
    dijkstra(s);
    return 0;
}